package com.hsh.gaoqianlianmeng.comparator;

import com.hsh.gaoqianlianmeng.domain.YangImport;

public class YangComparator {

    /**
     * 按商品编码序号排序，序号相同时按商品编码排序
     *
     * @param y1
     * @param y2
     * @return
     */
    public static int compare(YangImport y1, YangImport y2) {
        // 按序号排序
        Integer codeIndex1 = y1.getCodeIndex();
        Integer codeIndex2 = y2.getCodeIndex();

        // 处理序号为 null 的情况
        if (codeIndex1 == null && codeIndex2 == null) {
            return 0;
        }
        if (codeIndex1 == null) {
            return -1;
        }
        if (codeIndex2 == null) {
            return 1;
        }

        // 序号不相同，按序号排序
        int codeIndexComparison = codeIndex1.compareTo(codeIndex2);
        if (codeIndexComparison != 0) {
            return codeIndexComparison;
        }

        // 序号相同，按商品编码排序
        String productCode1 = y1.getProductCode();
        String productCode2 = y2.getProductCode();

        // 处理商品编码为 null 或空字符串的情况
        if (productCode1 == null && productCode2 == null) {
            return 0;
        }
        if (productCode1 == null) {
            return -1;
        }
        if (productCode2 == null) {
            return 1;
        }
        if (productCode1.isEmpty() && productCode2.isEmpty()) {
            return 0;
        }
        if (productCode1.isEmpty()) {
            return -1;
        }
        if (productCode2.isEmpty()) {
            return 1;
        }

        int productCodeComparison = -1 * productCode1.compareTo(productCode2);
        if (productCodeComparison != 0) {
            return productCodeComparison;
        }

        // 商品编码相同，按数量排序
        Integer num1 = y1.getNum();
        Integer num2 = y2.getNum();

        // 处理数量为 null 的情况
        if (num1 == null && num2 == null) {
            return 0;
        }
        if (num1 == null) {
            return -1;
        }
        if (num2 == null) {
            return 1;
        }

        return -1 * num1.compareTo(num2);
    }


}
